cmake_minimum_required(VERSION 3.16)

project(HLMLab LANGUAGES CXX)

# Qt setup
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find Qt5 (adjust Qt5_DIR when configuring if needed)
find_package(Qt5 REQUIRED COMPONENTS Widgets Core Gui Network OpenGL Xml PrintSupport)

# Paths
set(HLMLAB_ROOT "${CMAKE_SOURCE_DIR}/HLMLab")
set(HLMLAB_INC  "${HLMLAB_ROOT}/include")
set(HLMLAB_LIB  "${HLMLAB_ROOT}/lib")
set(HLMLAB_BIN  "${HLMLAB_ROOT}/bin")

# Imported FramelessHelper libraries (prebuilt in repo)
add_library(FramelessHelperCore SHARED IMPORTED)
set_target_properties(FramelessHelperCore PROPERTIES
  IMPORTED_IMPLIB_DEBUG   "${HLMLAB_LIB}/debug/FramelessHelperCore64d.lib"
  IMPORTED_IMPLIB_RELEASE "${HLMLAB_LIB}/release/FramelessHelperCore64.lib"
  IMPORTED_LOCATION_DEBUG   "${HLMLAB_BIN}/debug/FramelessHelperCore64d.dll"
  IMPORTED_LOCATION_RELEASE "${HLMLAB_BIN}/release/FramelessHelperCore64.dll"
)

add_library(FramelessHelperWidgets SHARED IMPORTED)
set_target_properties(FramelessHelperWidgets PROPERTIES
  IMPORTED_IMPLIB_DEBUG   "${HLMLAB_LIB}/debug/FramelessHelperWidgets64d.lib"
  IMPORTED_IMPLIB_RELEASE "${HLMLAB_LIB}/release/FramelessHelperWidgets64.lib"
  IMPORTED_LOCATION_DEBUG   "${HLMLAB_BIN}/debug/FramelessHelperWidgets64d.dll"
  IMPORTED_LOCATION_RELEASE "${HLMLAB_BIN}/release/FramelessHelperWidgets64.dll"
)

# Optional: Quick lib exists but is not needed for these widgets apps
# add_library(FramelessHelperQuick SHARED IMPORTED)
# set_target_properties(FramelessHelperQuick PROPERTIES
#   IMPORTED_IMPLIB_DEBUG   "${HLMLAB_LIB}/debug/FramelessHelperQuick64d.lib"
#   IMPORTED_IMPLIB_RELEASE "${HLMLAB_LIB}/release/FramelessHelperQuick64.lib"
#   IMPORTED_LOCATION_DEBUG   "${HLMLAB_BIN}/debug/FramelessHelperQuick64d.dll"
#   IMPORTED_LOCATION_RELEASE "${HLMLAB_BIN}/release/FramelessHelperQuick64.dll"
# )

# ---------------------------
# HLMLab main application
# ---------------------------
set(HLMLAB_APP_SOURCES
  ${HLMLAB_ROOT}/main.cpp
  ${HLMLAB_ROOT}/HLMLab.cpp
  ${HLMLAB_ROOT}/Common.cpp
  ${HLMLAB_ROOT}/arrow.cpp
  ${HLMLAB_ROOT}/ModelScene.cpp
  ${HLMLAB_ROOT}/ModelView.cpp
  ${HLMLAB_ROOT}/ModuleItem.cpp
  ${HLMLAB_ROOT}/ModuleManagerView.cpp
  ${HLMLAB_ROOT}/PanelEditorView.cpp
  ${HLMLAB_ROOT}/PortItem.cpp
  ${HLMLAB_ROOT}/PropertyDlg.cpp
  # UI forms (AutoUic)
  ${HLMLAB_ROOT}/HLMLab.ui
  ${HLMLAB_ROOT}/ModuleManagerView.ui
  ${HLMLAB_ROOT}/PanelEditorView.ui
  ${HLMLAB_ROOT}/PanelManagerView.ui
  ${HLMLAB_ROOT}/ModelPanel.ui
  ${HLMLAB_ROOT}/CalculationEngine/AbsCalNode.cpp
  ${HLMLAB_ROOT}/CalculationEngine/AbsConnector.cpp
  ${HLMLAB_ROOT}/CalculationEngine/AbsIoNode.cpp
  ${HLMLAB_ROOT}/CalculationEngine/AbsSimuEngine.cpp
  ${HLMLAB_ROOT}/CalculationEngine/CacheNode.cpp
  ${HLMLAB_ROOT}/CalculationEngine/ConsNode.cpp
  ${HLMLAB_ROOT}/CalculationEngine/DisplayNode.cpp
  ${HLMLAB_ROOT}/CalculationEngine/GainNode.cpp
  ${HLMLAB_ROOT}/CalculationEngine/MultNode.cpp
  ${HLMLAB_ROOT}/CalculationEngine/NodeFactory.cpp
  ${HLMLAB_ROOT}/CalculationEngine/SineWaveNode.cpp
  ${HLMLAB_ROOT}/CalculationEngine/SumNode.cpp
  ${HLMLAB_ROOT}/HLMLab.qrc
)

if(WIN32)
  add_executable(HLMLabApp WIN32 ${HLMLAB_APP_SOURCES})
else()
  add_executable(HLMLabApp ${HLMLAB_APP_SOURCES})
endif()

target_include_directories(HLMLabApp PRIVATE
  ${HLMLAB_ROOT}
  ${HLMLAB_INC}
  ${HLMLAB_ROOT}/CalculationEngine
  ${CMAKE_CURRENT_BINARY_DIR}
)

target_link_libraries(HLMLabApp PRIVATE
  Qt5::Widgets
  Qt5::Core
  Qt5::Gui
  Qt5::Network
  Qt5::Xml
  FramelessHelperWidgets
  FramelessHelperCore
)

# Copy FramelessHelper DLLs next to executable after build
add_custom_command(TARGET HLMLabApp POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:HLMLabApp>"
  COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<IF:$<CONFIG:Debug>,${HLMLAB_BIN}/debug/FramelessHelperCore64d.dll,${HLMLAB_BIN}/release/FramelessHelperCore64.dll>" "$<TARGET_FILE_DIR:HLMLabApp>"
  COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<IF:$<CONFIG:Debug>,${HLMLAB_BIN}/debug/FramelessHelperWidgets64d.dll,${HLMLAB_BIN}/release/FramelessHelperWidgets64.dll>" "$<TARGET_FILE_DIR:HLMLabApp>"
)

# ---------------------------
# HLMHub application
# ---------------------------
set(HLMHUB_ROOT "${HLMLAB_ROOT}/HLMHub")

set(HLMHUB_SOURCES
  ${HLMHUB_ROOT}/main.cpp
  ${HLMHUB_ROOT}/HLMHub.cpp
  ${HLMHUB_ROOT}/SamplerView.cpp
  ${HLMHUB_ROOT}/qcustomplot.cpp
  # UI form
  ${HLMHUB_ROOT}/HLMHub.ui
  ${HLMHUB_ROOT}/HLMHub.qrc
)

if(WIN32)
  add_executable(HLMHubApp WIN32 ${HLMHUB_SOURCES})
else()
  add_executable(HLMHubApp ${HLMHUB_SOURCES})
endif()

target_include_directories(HLMHubApp PRIVATE
  ${HLMHUB_ROOT}
  ${HLMLAB_INC}
  ${CMAKE_CURRENT_BINARY_DIR}
)

target_link_libraries(HLMHubApp PRIVATE
  Qt5::Widgets
  Qt5::Core
  Qt5::Gui
  Qt5::Network
  Qt5::OpenGL
  Qt5::PrintSupport
  FramelessHelperWidgets
  FramelessHelperCore
)

add_custom_command(TARGET HLMHubApp POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:HLMHubApp>"
  COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<IF:$<CONFIG:Debug>,${HLMLAB_BIN}/debug/FramelessHelperCore64d.dll,${HLMLAB_BIN}/release/FramelessHelperCore64.dll>" "$<TARGET_FILE_DIR:HLMHubApp>"
  COMMAND ${CMAKE_COMMAND} -E copy_if_different "$<IF:$<CONFIG:Debug>,${HLMLAB_BIN}/debug/FramelessHelperWidgets64d.dll,${HLMLAB_BIN}/release/FramelessHelperWidgets64.dll>" "$<TARGET_FILE_DIR:HLMHubApp>"
)

# Optional: deployment helper for Qt on Windows (calls windeployqt if available)
if(WIN32)
  find_program(WINDEPLOYQT_EXECUTABLE windeployqt PATHS "$ENV{QTDIR}/bin" "$ENV{QT_DIR}/bin" "${Qt5_DIR}/../../../bin" NO_DEFAULT_PATH)
  if(WINDEPLOYQT_EXECUTABLE)
    add_custom_target(deploy_hlmlab
      COMMAND "${WINDEPLOYQT_EXECUTABLE}" --no-translations "$<TARGET_FILE:HLMLabApp>"
      DEPENDS HLMLabApp
      COMMENT "Running windeployqt for HLMLabApp")
    add_custom_target(deploy_hlmhub
      COMMAND "${WINDEPLOYQT_EXECUTABLE}" --no-translations "$<TARGET_FILE:HLMHubApp>"
      DEPENDS HLMHubApp
      COMMENT "Running windeployqt for HLMHubApp")
  endif()
endif()


